package com.cskaoyan.user.service.impl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.UserLoginOutResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cskaoyan.mall.commons.util.jwt.AESUtil;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.bo.UserLoginBo;
import com.cskaoyan.user.dto.vo.UserLoginVo;
import com.cskaoyan.user.service.UserLoginService;

import java.util.Date;

/**
 * @author zyf
 * @since 2022/06/17 17:07
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    KaptchaServiceImpl kaptchaService;
    @Autowired
    MemberMapper memberMapper;

    @Override
    public UserLoginVo loginPost(UserLoginBo userLoginBo, HttpServletRequest request) {
        UserLoginVo userLoginVo = new UserLoginVo();
        String userPwd = userLoginBo.getUserPwd();
        AESUtil aesUtil = new AESUtil(userPwd);
        String encrypt = aesUtil.encrypt();
        Long id = memberMapper.selectIdByName(userLoginBo.getUserName());
        String pwd = memberMapper.selectPwdById(id);
        if (id==null){return userLoginVo;}
        if (pwd.equals(encrypt)) {
            userLoginVo.setUid(id);
            userLoginVo.setUsername(userLoginBo.getUserName());
            return userLoginVo;
        } else {
            return userLoginVo;
        }
    }

    @Override
    public Member selectMember(UserLoginBo userLoginBo) {
        Long id = memberMapper.selectIdByName(userLoginBo.getUserName());
        Member member = memberMapper.selectByPrimaryKey(id);

        return member;
    }

    @Override
    public Long selectIdByUserName(String userName) {
        Long id= memberMapper.selectIdByName(userName);
        return id;
    }

    /**
     * @author wy
     * @since 2022/6/18 23:06
     * 实现功能...退出登录（清除token）
     */
    @Override
    public UserLoginOutResponse loginOut(HttpServletRequest request, HttpServletResponse response2) {
        UserLoginOutResponse userLoginOutResponse = new UserLoginOutResponse();
        try {

            String token = CookieUtil.getCookieValue(request, "access_token");

            Cookie[] cookies = request.getCookies();
            if(cookies != null){
                for (Cookie cookie : cookies) {
                    if("access_token".equals(cookie.getName())){
                        cookie.setMaxAge(0);
                        cookie.setPath("/");
                        response2.addCookie(cookie);
                    }
                }
            }

            userLoginOutResponse.setSuccess(true);
            userLoginOutResponse.setResult(null);
            userLoginOutResponse.setTimestamp(new Date());
            userLoginOutResponse.setCode(UserRetCode.SUCCESS.getCode());
            userLoginOutResponse.setMsg(UserRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(userLoginOutResponse,e);
        }
        return userLoginOutResponse;
    }
}
